//
//  DrawUtils.m
//  OpenGLES_03
//
//  Created by Keisuke Hata on 09/12/24.
//  Copyright 2009 Keisuke Hata. All rights reserved.
//

#import "DrawUtils.h"
#import "TextureUtils.h"

#define USE_ALPHA_TEST 0

@implementation DrawUtils

+ (void) drawWithTexture3D : (GLVector3D)vec 
				imageAnchor:(GLVector3D)anchor 
				imageRotate:(GLVector3D)rotate 
				imageAlpha:(float)alpha 
				imageScale:(CGSize)scale 
				texture:(Texture2D *)tex 
				isPVRTC:(BOOL)pvrflg {

NSAssert( tex != nil , @"Texture is empty" );

GLfloat x = vec.x;
GLfloat y = vec.y;
GLfloat z = vec.z;
GLfloat width  = tex.realwidth;
GLfloat height = tex.realheight;
GLfloat s = tex.realwidth/(GLfloat)tex.width;
GLfloat t = tex.realheight/(GLfloat)tex.height;

x = -anchor.x;
y = -anchor.y;

GLfloat vertices[] = 
{
	x      ,y,			z,
	x      ,y+height,	z,
	x+width,y,			z,
	
	x      ,y+height,	z,
	x+width,y,			z,
	x+width,y+height,	z,
};
GLubyte colors[] = 
{
	_RGBA( 255,255,255, 255*alpha ),
	_RGBA( 255,255,255, 255*alpha ),
	_RGBA( 255,255,255, 255*alpha ),
	
	_RGBA( 255,255,255, 255*alpha ),
	_RGBA( 255,255,255, 255*alpha ),
	_RGBA( 255,255,255, 255*alpha ),
};
GLfloat texcoords[] = 
{
	0.0,t,
	0.0,0.0,
	s,t,
	
	0.0,0.0,
	s,t,
	s,0.0,
};

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY);

glVertexPointer( 3 , GL_FLOAT , 0 , vertices );
glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colors );
glTexCoordPointer( 2, GL_FLOAT, 0, texcoords);

glDrawArrays(GL_TRIANGLES, 0, 3*2);

glDisableClientState( GL_TEXTURE_COORD_ARRAY );
glDisableClientState( GL_COLOR_ARRAY);
glDisableClientState( GL_VERTEX_ARRAY);	
}

+ (void) drawWithTexture2D : (CGPoint)pos 
				imageAnchor:(CGPoint)anchor 
				imageAngleZ:(float)angleZ 
				imageAlpha:(float)alpha 
				imageScale:(CGSize)scale 
				texture:(Texture2D *)tex 
				isPVRTC:(BOOL)pvrflg {

	NSAssert( tex != nil , @"Texture is empty" );
	
	GLfloat x = pos.x;
	GLfloat y = pos.y;
	GLfloat width  = tex.realwidth;
	GLfloat height = tex.realheight;
	GLfloat s = tex.realwidth/(float)tex.width;
	GLfloat t = tex.realheight/(float)tex.height;
	
	x = -anchor.x;
	y = -anchor.y;

	GLfloat vertices[] = 
	{
		x      ,y,
		x      ,y+height,
		x+width,y,
		
		x      ,y+height,
		x+width,y,
		x+width,y+height,
	};
	GLubyte colors[] = 
	{
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
		
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
	};
	GLfloat texcoords[] = 
	{
		0.0,0.0,
		0.0,t,
		s,0.0,
		
		0.0,t,
		s,0.0,
		s,t,
	};
	
	
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnableClientState( GL_VERTEX_ARRAY );
	glEnableClientState( GL_COLOR_ARRAY);
	
	glVertexPointer( 2 , GL_FLOAT , 0 , vertices );
	glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colors );
	glTexCoordPointer( 2, GL_FLOAT, 0, texcoords);
	
	glDrawArrays(GL_TRIANGLES, 0, 3*2);

	glDisableClientState( GL_TEXTURE_COORD_ARRAY );
	glDisableClientState( GL_COLOR_ARRAY);
	glDisableClientState( GL_VERTEX_ARRAY);	

}

+ (void) drawWithTexture2DFBO : (CGPoint)pos 
				imageAnchor:(CGPoint)anchor 
				imageAlpha:(float)alpha 
				imageOffset:(CGPoint)offset
				imageSize:(CGSize)size 
				texture:(Texture2D *)tex 
				isPVRTC:(BOOL)pvrflg {
	
	NSAssert( tex != nil , @"Texture is empty" );
	
	GLfloat x = pos.x;
	GLfloat y = pos.y;
	GLfloat width  = size.width;
	GLfloat height = size.height;
	GLfloat s = size.width/(float)tex.width;
	GLfloat t = size.height/(float)tex.height;
	GLfloat offs_s = ( offset.x / (float)tex.width);
	GLfloat offs_t = (tex.realheight/(float)tex.height) - t - ( offset.y/ (float)tex.height);
	
	x = -anchor.x;
	y = -anchor.y;
	
	GLfloat vertices[] = 
	{
		x      ,y,
		x      ,y+height,
		x+width,y,
		
		x      ,y+height,
		x+width,y,
		x+width,y+height,
	};
	GLubyte colors[] = 
	{
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
		
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
	};
	GLfloat texcoords[] = 
	{
		offs_s     ,t + offs_t,
		offs_s     ,offs_t,
		s + offs_s ,t + offs_t,
		
		offs_s     ,offs_t,
		s + offs_s ,t + offs_t,
		s + offs_s ,offs_t,
	};
	
	
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnableClientState( GL_VERTEX_ARRAY );
	glEnableClientState( GL_COLOR_ARRAY);
	
	glVertexPointer( 2 , GL_FLOAT , 0 , vertices );
	glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colors );
	glTexCoordPointer( 2, GL_FLOAT, 0, texcoords);
	
	glDrawArrays(GL_TRIANGLES, 0, 3*2);
	
	glDisableClientState( GL_TEXTURE_COORD_ARRAY );
	glDisableClientState( GL_COLOR_ARRAY);
	glDisableClientState( GL_VERTEX_ARRAY);	
	
}


+ (void) drawWithTexture2DAtlas : (CGPoint)pos 
				imageAnchor:(CGPoint)anchor 
				imageAngleZ:(float)angleZ 
				imageAlpha:(float)alpha 
				imageScale:(CGSize)scale 
				imageOffset:(CGPoint)offs
				imageSize:(CGSize)size
				texture:(Texture2D *)tex 
				isPVRTC:(BOOL)pvrflg {
	
	NSAssert( tex != nil , @"Texture is empty" );
	
	GLfloat x		= pos.x;
	GLfloat y		= pos.y;
	GLfloat width	= size.width;
	GLfloat height	= size.height;
	GLfloat s		= offs.x / tex.width;
	GLfloat t		= offs.y / tex.height;
	GLfloat ss      = size.width / tex.width;
	GLfloat ts      = size.height / tex.height;
	
	x = -anchor.x;
	y = -anchor.y;
	
	GLfloat vertices[] = 
	{
		x      ,y,
		x      ,y+height,
		x+width,y,
		
		x      ,y+height,
		x+width,y,
		x+width,y+height,
	};
	GLubyte colors[] = 
	{
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
		
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
	};
	GLfloat texcoords[] = 
	{
		s		,t,
		s		,t+ts,
		s+ss	,t,
		
		s		,t+ts,
		s+ss	,t,
		s+ss	,t+ts,
	};
	
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnableClientState( GL_VERTEX_ARRAY );
	glEnableClientState( GL_COLOR_ARRAY);
	
	glVertexPointer( 2 , GL_FLOAT , 0 , vertices );
	glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colors );
	glTexCoordPointer( 2, GL_FLOAT, 0, texcoords);
	
	glDrawArrays(GL_TRIANGLES, 0, 3*2);
	
	glDisableClientState( GL_TEXTURE_COORD_ARRAY );
	glDisableClientState( GL_COLOR_ARRAY);
	glDisableClientState( GL_VERTEX_ARRAY);	
	
}


+ (void) drawWithTexture2DWithUVScale : (CGPoint)pos 
					imageAnchor:(CGPoint)anchor 
					imageAngleZ:(float)angleZ 
					imageAlpha:(float)alpha 
					imageScale:(CGSize)scale 
					imageUVScale:(float)uvs 
					texture:(Texture2D *)tex  
					isPVRTC:(BOOL)pvrflg {
	
	NSAssert( tex != nil , @"Texture is empty" );
	
	GLfloat x = pos.x;
	GLfloat y = pos.y;
	GLfloat width  = tex.realwidth;
	GLfloat height = tex.realheight;
	GLfloat s = tex.realwidth/(float)tex.width;
	GLfloat t = tex.realheight/(float)tex.height;
	
	x = -anchor.x;
	y = -anchor.y;
	
	GLfloat vertices[] = 
	{
		x      ,y,
		x      ,y+height,
		x+width,y,
		
		x      ,y+height,
		x+width,y,
		x+width,y+height,
	};
	GLubyte colors[] = 
	{
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
		
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
		_RGBA( 255,255,255, 255*alpha ),
	};
	GLfloat texcoords[] = 
	{
		0.0,0.0,
		0.0,t*uvs,
		s*uvs,0.0,
		
		0.0,t*uvs,
		s*uvs,0.0,
		s*uvs,t*uvs,
	};
	
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnableClientState( GL_VERTEX_ARRAY );
	glEnableClientState( GL_COLOR_ARRAY);
	
	glVertexPointer( 2 , GL_FLOAT, 0, vertices );
	glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colors );
	glTexCoordPointer( 2, GL_FLOAT, 0, texcoords);
	
	glDrawArrays(GL_TRIANGLES, 0, 3*2);
	
	glDisableClientState( GL_TEXTURE_COORD_ARRAY );
	glDisableClientState( GL_COLOR_ARRAY);
	glDisableClientState( GL_VERTEX_ARRAY);	
}


@end
